<template>
  <div class="dashboard-container">
    <div class="app-container">

      <PageTool>
        <template #before>
          <el-button type="primary" @click="addFn">新增分类</el-button>
        </template>
        <template #after>
          <el-input v-model="isKeyword" placeholder="请输入关键字">
            <el-button slot="append" icon="el-icon-search" @click="search" />
          </el-input>
        </template>
      </PageTool>

      <!-- <el-table :data="list" border>
        <el-table-column prop="image" label="图片" sortable="" />
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="desc" label="介绍" />
        <el-table-column prop="title" label="操作">
          <template v-slot="{row}">
            <el-button type="primary" @click="editFn(row.id)">编辑</el-button>
            <el-button type="danger" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center">
        <el-pagination
          :current-page.sync="page"
          :page-size="5"
          layout="total,prev, pager, next"
          :total="total"
          @current-change="paging"
        />
      </el-row> -->

      <el-table :data="list" border>
        <el-table-column prop="image" label="图片" sortable="">
          <template v-slot="{row}">
            <img
              v-imgError="require('@/assets/common/bigUserHeader.png')"
              style="width: 100px"
              :src="row.image"
              alt=""
            >
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" />
        <el-table-column prop="desc" label="介绍" />
        <el-table-column prop="title" label="操作">
          <template v-slot="{row}">
            <el-button size="small" type="primary" @click="editFn(row.id)">编辑</el-button>
            <el-button size="small" type="danger" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-card class="pagination">
        <el-row type="flex" justify="center">
          <el-pagination
            :current-page.sync="page"
            :page-size="pageSize"
            layout="total,prev, pager, next"
            :total="total"
            @current-change="paging"
          />
        </el-row>
      </el-card>
      <addFn ref="datas" :show-dialog="visidleDialog" @closeEvent="closeFn" @updateEvent="getClassLists" />
    </div>
  </div>

</template>

<script>
import { getClassList, delClass, totalClass } from '@/api/classify'
import addFn from './component/addFn.vue'
export default {
  components: {
    addFn
  },
  data() {
    return {
      page: 1,
      start: 0,
      list: [],
      visidleDialog: false,
      isKeyword: '',
      total: 0,
      pageSize: 4
    }
  },
  created() {
    this.getClassLists()
  },
  methods: {
    // 搜索查询
    async search() {
      await this.getClassLists()
      const res = await totalClass({ title_contains: this.isKeyword })
      this.total = res.data
    },
    // 点击编辑
    async editFn(id) {
    // 开启模态框
      this.visidleDialog = true
      // 传id实现回显
      this.$refs.datas.getEditDates(id)
    },
    // 点击删除
    delFn(id) {
      this.$confirm('此操作将永久删除该文件,是否继续', {
        type: 'warning'
      }).then(async() => {
        // 点击删除确定后调用删除接口
        await delClass(id)
        // 还有可以优化一下 刷完之后的nodata 的清空  未做
        this.getClassLists()
      }).catch(() => {
        console.log('失败')
      })
    },
    // 关闭弹窗
    closeFn(val) {
      this.visidleDialog = val
    },
    // 添加分类
    addFn() {
      // console.log('添加')
      this.visidleDialog = true // 显示模态框
    },
    // 封装渲染列表
    async  getClassLists() {
      const res = await getClassList({
        _limit: this.pageSize, //
        _start: 0,
        title_contains: this.isKeyword
      })
      // console.log(53, res)
      this.list = res.data
      const rea = await totalClass()
      // console.log('总条数', rea)
      this.total = rea.data
    },
    // 分页跳转 未跳转
    async paging(value) {
      // console.log(64, value)
      // this.page = value/
      const res = await getClassList({
        _limit: this.pageSize, //
        _start: this.pageSize * (value - 1) // 从哪里开始拿两条

      })
      this.list = res.data
    }
  }
}
</script>

<style>
</style>
